package com.shop.cereshop.app.tencent.im.service.impl;

import com.shop.cereshop.app.tencent.im.model.msg.BaseSendMsgParam;
import com.shop.cereshop.app.tencent.im.model.notice.SendNoticeParam;
import com.shop.cereshop.app.tencent.im.service.MsgService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.tencent.im.enums.im.ActionStatus;
import com.shop.cereshop.commons.tencent.im.enums.im.MsgTypeEnum;
import com.shop.cereshop.commons.tencent.im.model.msg.data.CustomMsgData;
import com.shop.cereshop.commons.tencent.im.model.msg.data.MsgData;
import com.shop.cereshop.commons.tencent.im.model.msg.dto.MsgRequestDTO;
import com.shop.cereshop.commons.tencent.im.model.response.SendMsgResponse;
import com.shop.cereshop.commons.tencent.im.service.TimMsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @description: 腾讯云IM 消息接口实现
 * @author zdd
 * @date 2025/9/3 14:30
 * @version 1.0
 */
@Slf4j
@Service
public class MsgServiceImpl implements MsgService {

    @Resource
    private TimMsgService timMsgService;

    @Override
    public Result<Void> sendC2cMsg(BaseSendMsgParam<MsgData> param) {
        MsgTypeEnum msgTypeEnum = MsgTypeEnum.getByType(param.getMsgType());
        if (msgTypeEnum == null) {
            return Result.custom(CoReturnFormat.FAIL, "消息类型错误!");
        }
        MsgRequestDTO requestDTO = MsgRequestDTO.builder()
                .fromAccount(param.getSenderId())
                .toAccount(param.getReceiverId())
                .groupId(param.getGroupId())
                .build();
        SendMsgResponse response = timMsgService.sendC2cMsg(msgTypeEnum, param.getMsgData(), requestDTO);
        if (ActionStatus.OK.getStatus().equals(response.getActionStatus())) {
            return Result.success();
        } else {
            return Result.custom(CoReturnFormat.FAIL, "系统繁忙，请稍后再试!");
        }
    }

    @Override
    public Result<Void> sendSysNotice(SendNoticeParam param) {
        CustomMsgData customMsgData = CustomMsgData.builder()
                .data(param.covertToMsgData())
                .desc(param.buildNoticeTitle())
                .build();
        MsgRequestDTO requestDTO = MsgRequestDTO.builder()
//                .fromAccount("administrator")
                .fromAccount("sendSignal")
                .toAccount(param.getReceiverId())
                .groupId(param.getGroupId())
                .build();
        SendMsgResponse response = timMsgService.sendC2cMsg(MsgTypeEnum.CUSTOM, customMsgData, requestDTO);
        if (ActionStatus.OK.getStatus().equals(response.getActionStatus())) {
            return Result.success();
        } else {
            return Result.custom(CoReturnFormat.FAIL, "系统繁忙，请稍后再试!");
        }
    }

    @Async
    @Override
    public void sendNotice(Long receiverId, Integer type, Integer targetType, String targetId, Long actorId, String actorNick) {
        try {
            SendNoticeParam param = new SendNoticeParam()
                    .setReceiverId(String.valueOf(receiverId))
                    .setType(type)
                    .setTargetType(targetType)
                    .setTargetId(targetId)
                    .setActorId(String.valueOf(actorId))
                    .setActorNick(actorNick);
            sendSysNotice(param);
        } catch (Exception e) {
            log.error("发送通知失败: receiverId={}, type={}", receiverId, type, e);
        }
    }


}
